﻿Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Runa.Resources.ResourceManager
Imports Runa.UI.Effects
Imports Runa.UI.Animation
Imports Runa.UI.Metadata

Namespace Global.Runa.UI.Controls

    Public MustInherit Class ScrollBar
        Inherits Panel

        Private m_value As Single = 0
        Private m_ForegroundColor As Color = Color.Green
        Private m_BackgroundColor As Color = Color.Gray

        <XamlAware(IsPlainType:=True, ElementName:="MaxValue")>
        Public Property MaxValue() As Single = 100

        <XamlAware(IsPlainType:=True, ElementName:="MinValue")>
        Public Property MinValue() As Single = 0

        <XamlAware(IsPlainType:=True, ElementName:="Value")>
        Public Property Value() As Single
            Get
                Return Me.m_value
            End Get
            Set(value As Single)
                If value >= Me.MinValue And value <= Me.MaxValue Then
                    Me.m_value = value
                    Dim x = Me.Size.X * (Me.Value / (Me.MaxValue - Me.MinValue))
                    With Me.ProgressBarRectangle
                        .DrawBounds = New Rectangle(.AbsoluteBounds.X, .AbsoluteBounds.Y, x, Me.Size.Y)
                    End With
                Else
                    Throw New ArgumentException("Invalid value for Value.")
                End If
            End Set
        End Property

        <XamlAware(IsPlainType:=False, ElementName:="Foreground", Initializer:=GetType(Texture2DObjectConverter))>
        Public Property Foreground() As Texture2DObject
            Get
                Return Me.ProgressBarRectangle.Foreground
            End Get
            Set(value As Texture2DObject)
                Me.ProgressBarRectangle.Foreground = value
            End Set
        End Property

        <XamlAware(IsPlainType:=False, ElementName:="Size", Initializer:=GetType(Vector2Converter))>
        Public Overrides Property Size() As Microsoft.Xna.Framework.Vector2
            Get
                Return MyBase.Size
            End Get
            Set(value As Microsoft.Xna.Framework.Vector2)
                MyBase.Size = value
                Me.ProgressBarRectangle.Size = value
                Me.UpdateLayout()
            End Set
        End Property

        <XamlAware(IsPlainType:=False, ElementName:="ForegroundColor", Initializer:=GetType(ColorConverter))>
        Public Property ForegroundColor() As Color
            Get
                Return Me.m_ForegroundColor
            End Get
            Set(value As Color)
                Me.m_ForegroundColor = value
                Me.UpdateLayout()
            End Set
        End Property

        <XamlAware(IsPlainType:=False, ElementName:="BackgroundColor", Initializer:=GetType(ColorConverter))>
        Public Property BackgroundColor() As Color
            Get
                Return Me.m_BackgroundColor
            End Get
            Set(value As Color)
                Me.m_BackgroundColor = value
                Me.UpdateLayout()
            End Set
        End Property

        Private Property ProgressBarRectangle() As Image

        Public Sub New(ByVal Parent As UIElementBase)
            MyBase.New("", Parent)
            Me.Name = Me.GetType().Name & "_" & BytesToString(Guid.NewGuid().ToByteArray())
        End Sub

        Public Sub New(ByVal Name As String, ByVal Parent As UIElementBase)
            MyBase.New(Name, Parent)
        End Sub

        Protected Overrides Sub Initialize()
            Me.ProgressBarRectangle = New Image(Me) With {.Location = New Vector2(0, 0)}
            Me.MinValue = 0
            Me.MaxValue = 100
            Me.Value = 0
            Me.BackgroundColor = Color.Gray
            MyBase.Initialize()
        End Sub

        Protected Overrides Sub Dispose(disposing As Boolean)
            Me.Children.Clear()
            Me.ProgressBarRectangle.Dispose()
            MyBase.Dispose(disposing)
        End Sub

        Public Overrides Sub UpdateLayout()
            If Me.Size.X > 0 And Me.Size.Y > 0 Then
                Me.Background = Texture2DObject.GetPureColorTextureObject(Me.BackgroundColor, Me.Size.X, Me.Size.Y, Me.Game.GraphicsDevice)
            End If
            If Not (Me.ProgressBarRectangle.Foreground IsNot Nothing AndAlso Me.ProgressBarRectangle.Foreground.Texture IsNot Nothing) Then
                If Me.Size.X > 0 And Me.Size.Y > 0 Then
                    Me.ProgressBarRectangle.Foreground = Texture2DObject.GetPureColorTextureObject(Me.ForegroundColor, Me.Size.X, Me.Size.Y, Me.Game.GraphicsDevice)
                End If
            Else
                If Me.Size.X > 0 And Me.Size.Y > 0 Then
                    Me.ProgressBarRectangle.Foreground = Texture2DObject.GetPureColorTextureObject(Me.ForegroundColor, Me.Size.X, Me.Size.Y, Me.Game.GraphicsDevice)
                End If
            End If
            MyBase.UpdateLayout()
        End Sub

        Public Overrides Sub Draw(SpriteBatch As Microsoft.Xna.Framework.Graphics.SpriteBatch)
            MyBase.Draw(SpriteBatch)
        End Sub

    End Class

End Namespace